package controller;


import activiti.MyBean;
import org.activiti.engine.ManagementService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import po.ErrorCodeApply;
import service.ErrorCodeService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TicketController {

    @Autowired
    private ErrorCodeService errorCodeServiceImpl;

    @Autowired
    TaskService taskservice;


    @Autowired
    private ManagementService managementService;


    @RequestMapping("/dropTables")
    @ResponseBody
    public String dropTables(){
        managementService.executeCommand(commandContext -> {
            commandContext.getDbSqlSession().dbSchemaDrop();
            return null;
        });
        return "true";
    }

    @RequestMapping("/ticket")
    @ResponseBody
    public String ticket(){
        MyBean myBean = new MyBean();

        String instockUser = "I1";
        Map<String,Object> variables=new HashMap<String, Object>();
        String approve = "Bulky";
        variables.put("reasonCode ", approve);
        variables.put("applyuserid", instockUser);

        ErrorCodeApply apply = new ErrorCodeApply();
        errorCodeServiceImpl.startWorkflow(apply, instockUser, variables);

        List<Task> list = taskservice.createTaskQuery().processInstanceId(apply.getProcess_instance_id()).list();
        Task currentTask = list.get(0);

        variables=new HashMap<String,Object>();
        variables.put("myBean", myBean);
        String userid = "nan.wang";
        String taskid = currentTask.getId();
        System.out.println("Task Id:" + taskid);

        taskservice.claim(taskid, userid);
        taskservice.complete(taskid, variables);
        return "success";
    }
}
